//
//  StatusPictureView.swift
//  sinaweibowj
//
//  Created by Macintosh on 15/12/21.
//  Copyright © 2015年 mactoinsh. All rights reserved.
//

import UIKit
import SnapKit

let pictureCellMarigin :CGFloat = 5
let pictureCellID  = "pictureCellID"

class StatusPictureView: UICollectionView {
    
    //MARK:定义外部模型属性
    var imageURL:[NSURL]?{
        didSet{
            
            //修改配图视图大小
            let pSize = scalePictureView()
            //更新配图视图的大小
            //视图对应的属性如果有相关的属性就更新，如果没有就创建
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            
            //修改测试的文案
            testLabel.text = "\(imageURL?.count ?? 0)"
            
            //刷新列表
            reloadData()
        }
    }
    
    //MARK:重写构造方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //实例化一个流水布局
        let layout = UICollectionViewFlowLayout()
        //设置间距
        layout.minimumLineSpacing = pictureCellMarigin
        layout.minimumInteritemSpacing = pictureCellMarigin
        
        super.init(frame: frame, collectionViewLayout: layout)
        
        //注册
        self.registerClass(PictureCell.self, forCellWithReuseIdentifier: pictureCellID)
        
        //设置数据源
        self.dataSource = self
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /*
    单图会按照图片等比例显示
    多图的图片大小固定
    多图如果是4张，会按照 2 * 2 显示
    多图其他数量，按照 3 * 3 九宫格显示
    */
    
    //计算配图视图的大小
    private func scalePictureView()->(CGSize){
        
        //图片的数量
        let imageCount = imageURL?.count ?? 0
        
        //获取配图视图的最大宽度
        let maxWidth = screenW - StatusCellMarigin * 2
        let itemWidth = (maxWidth - 2 * pictureCellMarigin) / 3
        
        //取出collectionView的流水布局
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        if imageCount == 0 {
            return CGSizeZero
        }
        
        if imageCount == 1 {
            //先给固定尺寸
            let imageSize = CGSize(width: 180, height: 120)
            //设置单张的  itemSize
            layout.itemSize = imageSize
            return imageSize
        }
        if imageCount == 4 {
            return CGSize(width: itemWidth * 2 + pictureCellMarigin, height: itemWidth * 2 + pictureCellMarigin)
        }
        
        //程序走到这里就是其他的多张图片
        //1 2 3   (_-1)/3+1
        //4 5 6
        //7 8 9
        let row = CGFloat((imageCount-1)/3 + 1)
        let pictureSize = CGSize(width:maxWidth , height: row * itemWidth + (row - 1) * pictureCellMarigin )
        return pictureSize
        
    }
    
    
    
    //MARK:设置页面和布局
    private func setupUI(){
        
        self.addSubview(testLabel)
        
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
        
    }
    
    //MARK:懒加载所有的子视图
    private lazy var testLabel:UILabel = UILabel(title: "", color: UIColor.redColor(), fontSize: 15, margin: 20)
}


//MARK:实现数据源方法
extension StatusPictureView:UICollectionViewDataSource{
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURL?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //使用这个方法必须注册
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as!PictureCell
        //cell.backgroundColor = UIColor.randomColor()
        cell.imageURl = imageURL![indexPath.item]
        return cell
    }
    
}


class  PictureCell:UICollectionViewCell{

    //MARK:定义外部模型属性
    var imageURl:NSURL?{
        didSet{
        iconView.sd_setImageWithURL(imageURl)
        }
    }
    
    //MARK:重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:设置页面和布局
    private func setupUI(){
    
        contentView.addSubview(iconView)
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
    }
    
    //MARK:懒加载所有的子视图
    private lazy var iconView: UIImageView = {
    
        let v = UIImageView()
        //ScaleAspectFill  显示图片的原比例，图片会被裁剪，一般常见
        //ScaleToFill   拉神与压缩  默认显示样式
        v.contentMode = UIViewContentMode.ScaleAspectFill
        //在手码开发中，图片剪辑默认是没有开启的，需要手动设置
        v.clipsToBounds = true
        
        return v
    }()


}
